package com.renbang.test.quartz.demo2;  
  
import static org.quartz.JobBuilder.newJob;  
import static org.quartz.TriggerBuilder.newTrigger;  

import java.util.UUID;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;  
import org.quartz.JobKey;
import org.quartz.Scheduler;  
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;  
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;

import com.renbang.utils.drawandindex.Word;
  
public class QuartzScheduleMain {  
	private static Scheduler scheduler =getScheduler();
    public void start() throws Exception {  
    	String job1= "job1"+UUID.randomUUID();
    	String trigger1="trigger1"+UUID.randomUUID();
        //从调度管理器中获取调度对象  
        // define the job and tie it to our HelloJob class  
        //创建相关的job信息  
        JobDetail job = newJob(HelloJob.class)  
            .withIdentity(job1, "group1")  
            .build();  
        //创建一个触发器的名称  
//        Trigger trigger = newTrigger()  
//            .withIdentity("trigger1", "group1")  
//            .startAt(runTime)  
//            .build();  
        Trigger trigger = newTrigger()  
              .withIdentity(trigger1, "group1")  .withSchedule(
				CronScheduleBuilder
				.cronSchedule("0/3 * * * * ?")).build();
        //设置调度相关的Job  
        scheduler.scheduleJob(job, trigger);  
        //启动调度任务  
        scheduler.start();  
    }  
    //修改触发器
    public void modifyJobTime() throws Exception {  
        Trigger trigger = newTrigger()  
              .withIdentity("trigger1", "group1")  .withSchedule(
				CronScheduleBuilder
				.cronSchedule("0/10 * * * * ?")).build();
        scheduler.resumeTrigger(trigger.getKey());
//      scheduler.resumeTrigger("trigger1", "group1");
    } 
    
    //暂停任务
    public void pause() throws  Exception{
    	JobKey jobkey = new JobKey("job1", "group1");
    	scheduler.pauseJob(jobkey); 
    }
    //重启任务
    public void resume() throws  Exception{
    	JobKey jobkey = new JobKey("job1", "group1");
    	scheduler.resumeJob(jobkey); 
    }
    // 移除任务
    public void removeJob() throws  Exception{
    	JobKey jobkey = new JobKey("job1", "group1");
    	TriggerKey triggerKey = new TriggerKey("trigger1", "group1");
    	scheduler.pauseTrigger(triggerKey);//停止触发器  
    	scheduler.unscheduleJob(triggerKey);//移除触发器  
    	scheduler.deleteJob(jobkey); //删除任务  
    }
    //关闭调度
    public void shutdown() throws  Exception{
    	scheduler.shutdown(true);
    }
    
    public static void main(String[] args) throws Exception {  
  
        QuartzScheduleMain example = new QuartzScheduleMain();  
        example.start();  
  
    }
    
    
    private static Scheduler getScheduler() {  
        SchedulerFactory sf = new StdSchedulerFactory();  
        Scheduler scheduler=null;  
        try {  
            scheduler = sf.getScheduler();  
        } catch (SchedulerException e) {  
            e.printStackTrace();  
        }  
        return scheduler;  
} 
    public static Scheduler getInstanceScheduler() {  
        return scheduler;  
}
  
}  